<ul class="nav nav-list bs-docs-sidenav" style="width: 250px">
    <li class="nav-header nav-header-cd" style="font-size:20px; color: #222222;margin:10px 0 5px 0;">项目</li>
    <li class="nav-header nav-header-class" style="font-size:20px; color: #222222;margin:10px 0 5px 0;">类<i class="icon-plus-sign pull-right curse_pointer" style="display:none;margin-right: 12px" title="点击加号添加"></i></li>
    <li class="nav-header nav-header-relation" style="font-size:20px; color: #222222;margin:10px 0 5px 0;">类之间的关系<i class="icon-plus-sign pull-right curse_pointer" style="display:none;margin-right: 12px" title="点击加号添加"></i></li>
</ul>


<script type="text/javascript">
    // active initial
    $(function(){
        var alert_name;
        var alert_type = [];
        var p_delete_element;

        $(".icon-remove-sign-right").live("click",function(){

            p_delete_item= $(this).parent().parent();
            alert_name = $(p_delete_item).attr("name");

            if($(p_delete_item).hasClass("cd"))              alert_type = ['cd','项目'];
            else if($(p_delete_item).hasClass("class"))      alert_type = ['class','类'];
            else if($(p_delete_item).hasClass("relation"))   alert_type = ['relation','类之间的关系'];

            $('#modal-alert-content').modal('show');
        });

        $(".icon-plus-sign").live("click",function(){
            if($(this).parent().text() == "类"){
                $('#modal-create-class').children(".modal-body").children("input[type='text']").val('');
                $('#modal-create-class').modal('show');
            }
            else if($(this).parent().text() == "类之间的关系"){
                $('#modal-create-relation').children(".modal-body").children().children(".input-1").val('');
                $('#modal-create-relation').children(".modal-body").children().children(".input-2").val('');
                $('#modal-create-relation').modal('show');
            }
        });

        $('#modal-alert-content').on('show', function () {
            $("#modal-alert-content").children(".modal-body").children("h5").text("确定删除"+alert_type[1]+":"+alert_name+"?");
        });

        $('#delete-content').live('click',function(){
            $.ajax({
                url: '/post/data/icd-element',
                type: 'post',
                data: {
                    statusArray:statusArray,
                    type : alert_type[0],
                    process : 'remove',
                    data: {
                        new:alert_name,
                        sub:icd[alert_type[0]][alert_name]
                    }
                },
                dataType: 'json',
                async: true,
                success: function(doc){
                    if(alert_type[0] === 'cd'){
                        alert("项目删除成功");
                        return location.href = '/';
                        //return self.location= '/';
                    }
                    icd =doc.icd;
                    if(doc.count != 1) return alert("操作失败，请刷新页面后重新尝试");
                    else $(p_delete_item).remove();
                    getSourceNav();
                }
            });
        });

        $('#create-class').live("click",function(){
            var item_val = $(this).parent().parent().parent().children(".modal-body").children("input[type='text']").val();
            if(item_val.replace( /^[" "|"　"]*/, "") === "") return alert("输入不能为空");
            //判断是否存在
            //否则创建
            var item;
            if(icd['class'][item_val] != undefined){
                item =$(".span3 .nav-classdiagram").children("ul").children("li[name='"+item_val+"']");
                return clickOnNavItem(item);
            }else{
                for(var class_key in icd['class']){
                    if(icd['class'][class_key]["_value"][item_val] != undefined){
                        var items =$(".span3 .nav-classdiagram").children("ul").children();
                        for(var i=1;i<items.length;i++){
                            //alert($(items).eq(i).text());
                            if($(items).eq(i).text() === item_val){
                                item = $(items).eq(i);
                                return clickOnNavItem(item);
                            }
                        }
                    }
                }
            }
            $.ajax({
                url: '/post/data/icd-element',
                type: 'post',
                data: {
                    statusArray:statusArray,
                    type : 'class',
                    process : 'add',
                    data: {new:item_val}
                },
                dataType: 'json',
                async: true,
                success: function(doc){
                    icd = doc.icd ;
                    getSourceNav();
                    if(doc.count != 1) return alert("创建失败，请刷新页面后重新尝试");
                    //创建
                    var header_class = $(".nav.nav-list.bs-docs-sidenav").children(".nav-header-relation");
                    var html =  '<li name='+item_val+' class="class"><a href="javascript:;"><i class="icon-chevron-right "></i><i class="icon-remove-sign-right" title="点击叉号删除" style="display: none"></i>'+ item_val +'</a></li>';
                    $(header_class).before(html);
                    $(header_class).prev().trigger('click');
                }
            });
        });

        $('#create-relation').live("click",function(){
            var item_val1 = $(this).parent().parent().parent().children(".modal-body").children().eq(0).children("input[type='text']").val();
            var item_val2 = $(this).parent().parent().parent().children(".modal-body").children().eq(1).children("input[type='text']").val();
            if(item_val1.replace( /^[" "|"　"]*/, "") === "") return alert("输入不能为空");;
            if(item_val1.replace( /^[" "|"　"]*/, "") === "") return alert("输入不能为空");;
            var item_val = item_val1 +'-'+ item_val2;

            var mutex =2;
            for(var class_key in icd['class']){
                if(icd['class'][class_key]["_value"][item_val1] != undefined){
                    mutex--;
                }
                if(icd['class'][class_key]["_value"][item_val2] != undefined){
                    mutex--;
                }
                if(mutex === 0) break;
            };
            if(mutex !=0 ) return alert("无法创建!所输入类名不存在");


            var item;
            if(icd['relation'][item_val] != undefined){
                item =$(".span3 .nav-classdiagram").children("ul").children("li[name='"+item_val+"']");
                return clickOnNavItem(item);
            }else{
                for(var relation_key in icd['relation']){
                    if(icd['relation'][relation_key]["_value"][item_val] != undefined){
                        var items =$(".span3 .nav-classdiagram").children("ul").children();
                        for(var i=1;i<items.length;i++){
                            //alert($(items).eq(i).text());
                            if($(items).eq(i).text() === item_val){
                                item = $(items).eq(i);
                                return clickOnNavItem(item);
                            }
                        }
                    }
                }
            }

            $.ajax({
                url: '/post/data/icd-element',
                type: 'post',
                data: {
                    statusArray:statusArray,
                    type : 'relation',
                    process : 'add',
                    data: {new:item_val}
                },
                dataType: 'json',
                async: true,
                success: function(doc){
                    icd =doc.icd;
                    if(doc.count != 1) return alert("创建失败，请刷新页面后重新尝试");
                    //创建
                    getSourceNav();
                    var html = '<li name='+item_val+' class="relation"><a href="javascript:;"><i class="icon-chevron-right "></i><i class="icon-remove-sign-right" title="点击叉号删除" style="display: none"></i>'+ item_val +'</a></li>';
                    $(".nav.nav-list.bs-docs-sidenav").append(html);
                    //选中
                    $(".nav.nav-list.bs-docs-sidenav").children().trigger('click');
                }
            });
        });


        $("li").live("click",function(){
            mutex_input_dblclick = 0;
            if($(this).children().children().hasClass("icon-chevron-right")){
                $(this).parent().children().removeClass("active");
                $(this).addClass("active");
                var that = this;
                var url;
                var label;
                if($(this).hasClass("cd")){
                    url = '/post/html/icd-content-classdiagram';
                    label = ['cd','项目'];
                }
                else if($(this).hasClass("class")){
                    url = '/post/html/icd-content-class';
                    label = ['class','类'];
                }
                else if($(this).hasClass("relation")){
                    url = '/post/html/icd-content-relation';
                    label = ['relation','关系'];
                }
                else return;

                var name={
                    key :$(this).attr('name'),
                    value:$(this).text()
                };

                $.ajax({
                    url: url,
                    type: 'post',
                    success: function(html)
                    {
                        $(".content-detail").children().remove();
                        $(".content-detail").append(html);
                        //做数据处理
                        icd_content_show(label,name);
                        if(label[1]==="类") recommend("类名",name.value);
                        else recommend(label[1],name.value);
                    }
                });
            }
        });

        $(".bs-docs-sidenav li").live({
            mouseenter: function(){
                $(this).children("i").show();
                $(this).children("a").children("i").eq(1).show();
            },

            mouseleave: function(){
                $(this).children("i").hide();
                $(this).children("a").children("i").eq(1).hide();
            }
        });
    });
</script>